/*************************************************************************
 * File Name:    Longest_Palindromic_Substring.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013/11/6 23:47:00
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Longest Palindromic Substring
 |
 | Given a string S, find the longest palindromic substring in S.
 | You may assume that the maximum length of S is 1000, and there exists
 | one unique longest palindromic substring.
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

class Solution {
public:
    string longestPalindrome(string s)
    {
        int N = s.size();
        
        int left, right;
        string ans = "";
        for (int i = 0; i < N; ++i) {
            left = i - 1, right = i + 1;
            while (left >= 0 && right < N && s[left] == s[right]) {
                --left, ++right;
            }
            if (right - left - 1 > ans.size()) ans = s.substr(left + 1, right - left - 1);
            
            left = i, right = i + 1;
            while (left >= 0 && right < N && s[left] == s[right]) {
                --left, ++right;
            }
            if (right - left - 1 > ans.size()) ans = s.substr(left + 1, right - left - 1);
        }
        return ans;
    }
};

